package com.itboye.sunsun.login.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.itboye.homesensen.alert.ByAlert;
import com.itboye.sunsunhome.R;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.beans.PersonDataBean;
import com.itboye.sunsun.constants.SPContants;
import com.itboye.sunsun.controller.LoginController;
import com.itboye.sunsun.controller.LoginedState;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.DebugLog;
import com.itboye.sunsun.utils.SPUtils;
import com.itboye.sunsun.volley.CodeErrorException;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

public class ResetPasswordActivity extends BaseActivity implements OnClickListener {

	private ImageView back;
	private Button yzmbutton;
	private Button registbutton;
	private EditText yzmedit;
	private EditText phoneedit;
	private EditText newpasswordedit;
	private EditText confirmedit;
	private Button changebutton;
	private TimeCount time;
	private String username;
	protected String code;
	String passwordName;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_resetpass);
		
		time = new TimeCount(60000, 1000,yzmbutton);// 构造CountDownTimer对象
		setStatusBarColor(R.color.home_blue);
		
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			this.finish();
			break;
		case R.id.yzmbutton:
			username=phoneedit.getText().toString();
			if(!username.equals(""))
			{
			   getyzm();
			   time.start();
			}else
			{
				Toast.makeText(ResetPasswordActivity.this, "请输入手机号", 0).show();
			}
			break;
		case R.id.changebutton:
			 passwordName=newpasswordedit.getText().toString();
			if(passwordName==null || passwordName==""){
				ByAlert.alert("请输入密码");
			}else
//			if (newpasswordedit.getText().toString()==null || newpasswordedit.getText().toString().trim()=="") {
				changepassword();
			finish();
//			}else{
//				ByAlert.alert("验证码错误!");
//			}
		default:
			break;
		}

	}

	private void getyzm() {
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>().apiVer("100").typeKey("BY_Message_send_sms")
				.param("mobile", phoneedit.getText().toString()).param("code_type", "2")
				.param("app_type","2")
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());
						code=response.toString();
						DebugLog.v("LLL", code);
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, msg, 0).show();
						} else {
							Toast.makeText(App.ctx, exception.toString(), 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	private void changepassword() {
		
		
		if(yzmedit.getText().toString()==null){
			Toast.makeText(this, "请输入验证码", 0).show();
			return;
		}
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>().apiVer("100")
				.typeKey("BY_User_findPswByMobile")
				.param("mobile", phoneedit.getText().toString())
				.param("code", yzmedit.getText().toString())
				.param("psw", passwordName)
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());
						Toast.makeText(App.ctx, response, 0).show();

//						login();
						
						finish();

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, msg, 0).show();
						} else {
							Toast.makeText(App.ctx, exception.toString(), 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

	private void login() {
		MyJsonRequest<PersonDataBean> request = new MyJsonRequest.Builder<PersonDataBean>().apiVer("100")
				.typeKey("BY_User_login")
				.param("username", phoneedit.getText().toString())
				.param("password", newpasswordedit.getText().toString())
				.requestListener(new XRequestListener<PersonDataBean>() {

					@Override
					public void onResponse(PersonDataBean response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());

						SPUtils.put(App.ctx, null, SPContants.USERNAME, response.getUsername());
						SPUtils.put(App.ctx, null, SPContants.MOBILE, response.getMobile());
						SPUtils.put(App.ctx, null, SPContants.PASSWORD, newpasswordedit.getText().toString());
						SPUtils.put(App.ctx, null, SPContants.IS_LOGINED, true);
						SPUtils.put(App.ctx, null, SPContants.EMAIL, response.getEmail());
						SPUtils.put(App.ctx, null, SPContants.NICK, response.getNickname());

						SPUtils.put(App.ctx, null, SPContants.PASSWORD, response.getPassword());

						SPUtils.put(App.ctx, null, SPContants.USER_ID, response.getUid());
						SPUtils.put(App.ctx, null, SPContants.AVATAR_URL, response.getHead());

	
						//发送广播通知LoginActivity结束
						Intent intent1=new Intent(LoginActivity.LOGINEXIT);
						sendBroadcast(intent1);
						LoginController.setLoginState(new LoginedState());
						ResetPasswordActivity.this.finish();
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						if (exception instanceof CodeErrorException) {
							Toast.makeText(App.ctx, msg, 0).show();
						} else {
							Toast.makeText(App.ctx, exception.toString(), 0).show();
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}

}
